package business {
	import events.CurrencyFileHandlerEvent;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	import mx.utils.URLUtil;

	public class CurrencyFileHandler extends EventDispatcher {
		private static var _instance:CurrencyFileHandler;
		private var myLoader:URLLoader;
		public var currencyXML:XML;
		private var url:String;
		public var currencyContainer:CurrencyContainer;

		public function CurrencyFileHandler(access:Private) {
			if (access == null) {
				throw new Error("This is a singleton.. Use the getInstance method");
			}
		}

		public function downloadFile(url:String):void {
			var currencyXMLURL:URLRequest = new URLRequest(url);
			myLoader = new URLLoader(currencyXMLURL);
			myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
			myLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlLoadError);
		}

		public function xmlLoaded(event:Event):void {
			currencyXML = XML(myLoader.data);
			saveXMLToFile(currencyXML);
			processXML(currencyXML);

			dispatchCompleteEvent();
		}

		private function dispatchCompleteEvent():void {
			dispatchEvent(new CurrencyFileHandlerEvent(CurrencyFileHandlerEvent.EVENT_XML_READ_COMPLETE));
		}

		public function loadXMLFromFile():void {
			var file:File = File.applicationStorageDirectory;
			file = file.resolvePath("currency.xml");
			if (file.exists) {
				var stream:FileStream = new FileStream();
				stream.open(file, FileMode.READ);
				var xmlString:String = stream.readUTF();
				stream.close();
				processXML(XML(xmlString));
				dispatchCompleteEvent();
			}
		}

		private function saveXMLToFile(currencyXML:XML):void {
			var file:File = File.applicationStorageDirectory;
			file = file.resolvePath("currency.xml");
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTF(currencyXML.toXMLString());
			stream.close();
		}

		private function processXML(currencyXML:XML):void {
			default xml namespace = new Namespace("", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");

			currencyContainer = CurrencyContainer.getInstance();
			for each (var outer:XML in currencyXML..Cube) {
				currencyContainer.addCurrency(createCurrencyFromXML(outer));
			}
		}

		private function createCurrencyFromXML(currency:XML):Currency {
			var time:String = currency.@time;
			if (time != null && time.length > 0)
				currencyContainer.dateOfRates = time;

			var code:String = currency.@currency;
			if (code != null && code.length > 0) {
				var name:String = code; // currency.@desc;
				var rateString:String = currency.@rate;
				return new Currency(code, name, Number(rateString));
			}
			return null;
		}

		protected function xmlLoadError(event:IOErrorEvent):void {
//			trace("IO Error: " + event.toString());
		}

		public static function getInstance():CurrencyFileHandler {
			if (_instance == null) {
				_instance = new CurrencyFileHandler(new Private);
			}
			return _instance;
		}
	}
}

class Private {
}
